###############################################
#                                             #
#     GA Effects by party                     #
#                                             #
#     Created by Mac Lockhart May 16 2024     #
#     Updated May 16 2024                     #
#                                             #
###############################################

library(tidyverse)
library(data.table)
library(modelsummary)
library(kableExtra)
library(gt)
library(flextable)
library(car)

#Read in most recent version of cleaned data using:
#Clean_National_Sample.R
if(Sys.info()[7] == "maclockhart"){
  setwd("E:/Dropbox/School/RA Work/'22 Seth & Thad/MIT Evolving Election Administration Landscape")
}

DF <- fread("Survey Data/National Recoded.csv")
CO <- fread("Survey Data/Colorado Recoded.csv")
GA <- fread("Survey Data/Georgia Recoded.csv")
LA <- fread("Survey Data/LA Recoded.csv")
TX <- fread("Survey Data/Texas Recoded.csv")

DF$bot <- 0
DF$n<-1:nrow(DF)
#assign sample names
DF$sample<-"USA"
CO$sample<-"CO"
GA$sample<-"GA"
LA$sample <- "LA"
TX$sample <-"TX"

#Remove bots
DF<-rbindlist(list(DF, CO, GA, LA, TX), fill=T)
rm(CO, GA, LA, TX)
DF<-DF[bot==0]

#Recode outcomes as change from pre-treatment values following the pre-analysis plan
DF$ownstate <- DF$ownstatepost-DF$ownstatepre
DF$otherstates <- DF$otherstatepost - DF$otherstatepre
DF$votefraud <- DF$votefraudpost - DF$votefraudpre
DF$officialfraud <- DF$officialfraudpost - DF$officialfraudpre
DF$vote2024 <- DF$vote2024post - DF$vote2024pre

DF_full <-DF
DF <- DF_full[sample=="GA"]

##Treatment effects by party
#Pooled treatment effects by video
m1.1 <- lm(ownstate ~ d_treatment*party, DF)
m1.2 <- lm(otherstates ~ d_treatment*party, DF)
m1.3 <- lm(votefraud ~ d_treatment*party, DF)
m1.4 <- lm(officialfraud ~ d_treatment*party, DF)
m1.5 <- lm(vote2024 ~ d_treatment*party, DF)


binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Vote Fraud Belief" = m1.3,
                      "Officials Fraud Belief" = m1.4,
                      "2024 Vote Intent" = m1.5)
modelsummary(binary_models, output = "Mac and Jen Results/Treatment Effects/georgia_binary_byparty.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('d_treatment' = 'Treated',
                          "partyIndependent"="Independent",
                          "partyRepublican" = "Republican",
                          "d_treatment:partyIndependent" = "Treated X Independent",
                          "d_treatment:partyRepublican" = "Treated X Republican"),
             #coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
rm(m1.1, m1.2, m1.3, m1.4, m1.5)

#LA
DF <- DF_full[sample=="LA"]

##Treatment effects by party
#Pooled treatment effects by video
m1.1 <- lm(ownstate ~ d_treatment*party, DF)
m1.2 <- lm(otherstates ~ d_treatment*party, DF)
m1.3 <- lm(votefraud ~ d_treatment*party, DF)
m1.4 <- lm(officialfraud ~ d_treatment*party, DF)
m1.5 <- lm(vote2024 ~ d_treatment*party, DF)


binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Vote Fraud Belief" = m1.3,
                      "Officials Fraud Belief" = m1.4,
                      "2024 Vote Intent" = m1.5)
modelsummary(binary_models, output = "Mac and Jen Results/Treatment Effects/LA_binary_byparty.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('d_treatment' = 'Treated',
                          "partyIndependent"="Independent",
                          "partyRepublican" = "Republican",
                          "d_treatment:partyIndependent" = "Treated X Independent",
                          "d_treatment:partyRepublican" = "Treated X Republican"),
             #coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
rm(m1.1, m1.2, m1.3, m1.4, m1.5)

#TX
DF <- DF_full[sample=="TX"]

##Treatment effects by party
#Pooled treatment effects by video
m1.1 <- lm(ownstate ~ d_treatment*party, DF)
m1.2 <- lm(otherstates ~ d_treatment*party, DF)
m1.3 <- lm(votefraud ~ d_treatment*party, DF)
m1.4 <- lm(officialfraud ~ d_treatment*party, DF)
m1.5 <- lm(vote2024 ~ d_treatment*party, DF)


binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Vote Fraud Belief" = m1.3,
                      "Officials Fraud Belief" = m1.4,
                      "2024 Vote Intent" = m1.5)
modelsummary(binary_models, output = "Mac and Jen Results/Treatment Effects/texas_binary_byparty.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('d_treatment' = 'Treated',
                          "partyIndependent"="Independent",
                          "partyRepublican" = "Republican",
                          "d_treatment:partyIndependent" = "Treated X Independent",
                          "d_treatment:partyRepublican" = "Treated X Republican"),
             #coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
rm(m1.1, m1.2, m1.3, m1.4, m1.5)

#CO
DF <- DF_full[sample=="CO"]

##Treatment effects by party
#Pooled treatment effects by video
m1.1 <- lm(ownstate ~ d_treatment*party, DF)
m1.2 <- lm(otherstates ~ d_treatment*party, DF)
m1.3 <- lm(votefraud ~ d_treatment*party, DF)
m1.4 <- lm(officialfraud ~ d_treatment*party, DF)
m1.5 <- lm(vote2024 ~ d_treatment*party, DF)


binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Vote Fraud Belief" = m1.3,
                      "Officials Fraud Belief" = m1.4,
                      "2024 Vote Intent" = m1.5)
modelsummary(binary_models, output = "Mac and Jen Results/Treatment Effects/colorado_binary_byparty.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('d_treatment' = 'Treated',
                          "partyIndependent"="Independent",
                          "partyRepublican" = "Republican",
                          "d_treatment:partyIndependent" = "Treated X Independent",
                          "d_treatment:partyRepublican" = "Treated X Republican"),
             #coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
rm(m1.1, m1.2, m1.3, m1.4, m1.5)
